﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;
namespace Zor_Bll
{
    public class Zor_TenantEnterprise_Bll<T> where T : class
    {
        #region -个人或企业租户管理 -
        /// <summary>
        /// 获取租户列表
        /// </summary>
        /// <param name="Model">查询对象</param>
        /// <param name="index">页码</param>
        /// <param name="where">条件</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model, int index, out int TotalCount, string where)
        {
            return PagerHelp<T>.GetPagerList(index, "V_Zor_Tenant", "Tenant_CreateTime desc", out TotalCount, GetSearchWhere(Model, where));
        }

        /// <summary>
        /// 获取个人或企业管理列表（不分页）
        /// create zhl by 2017.4.1     
        /// </summary>
        /// <returns></returns>
        public static List<T> GetList(dynamic Model, string where)
        {
            return Zor_TenantEnterprise_DAL<T>.SelectViewTenant(GetSearchWhere(Model, where));
        }

        /// <summary>
        /// 查询所有行业
        /// create zhl by 2017.3.24
        /// <param name="Category">所属类别（1职业、2人口性质、3国籍4、行业）</param>
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectIndustry(int category)
        {
            return Zor_TenantEnterprise_DAL<T>.SelectIndustry(category);
        }

        /// <summary>
        /// 根据Id查询数据
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="tenant_Id">租户id</param>
        /// <returns></returns>
        public static List<T> SelectPageListId(string tenant_Id)
        {
            return Zor_TenantEnterprise_DAL<T>.SelectPageListId(tenant_Id);
        }
        /// <summary>
        /// 租户管理-新增
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="tenant">租户对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer InsertTenant(Zor_Tenant tenant)
        {
            tenant.Tenant_CreateTime = TimeHelper.ToTimeStamp(DateTime.Now);
            tenant.Tenant_CreateName = RequestSession.GetSessionUser().User_Id;
            tenant.Dt_Id = RequestSession.GetSessionUser().DT_Id;
            BaseAnswer bar = Zor_TenantEnterprise_DAL<T>.InsertTenant(tenant);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "增加成功！" : "增加失败,请检查当前租户是否已存在！";
            return bar;
        }

        /// <summary>
        /// 租户管理-修改
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="tenant">租户对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer UpdateTenant(Zor_Tenant tenant)
        {
            BaseAnswer bar = Zor_TenantEnterprise_DAL<T>.UpdateTenant(tenant);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "修改成功！" : "修改失败,请检查租户是否已存在！";
            return bar;

        }
        /// <summary>
        /// 删除租户
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="id">删除租户的id</param>
        /// <returns>1正常 返回0</returns>
        public static BaseAnswer DeleteTenant(int[] id)
        {
            BaseAnswer bar = Zor_TenantEnterprise_DAL<T>.DeleteTenant(id);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "删除成功！" : "删除失败，租赁管理存在对于的租户！";
            return bar;
        }

        #region 私有函数

        private static string GetSearchWhere(dynamic Model, string where)
        {
            var Dt_Id = RequestSession.GetSessionUser().DT_Id;
            var sqlSurch = string.Empty;

            if (Model.Tenant_Name != null)
            {
                sqlSurch += " and Tenant_Name like '%" + Model.Tenant_Name + "%'";
            }
            if (Model.sex != null)
            {
                sqlSurch += " and sex like '%" + Model.sex + "%'";
            }
            if (Model.Tenant_ProprietorPhone != null)
            {
                sqlSurch += " and Tenant_ProprietorPhone like '%" + Model.Tenant_ProprietorPhone + "%'";
            }
            if (Model.istaxregistration != null)
            {
                sqlSurch += " and istaxregistration like '%" + Model.istaxregistration + "%'";
            }
            if (Model.Tenant_TaxRegistrationNum != null)
            {
                sqlSurch += " and Tenant_TaxRegistrationNum like '%" + Model.Tenant_TaxRegistrationNum + "%'";
            }
            if (Model.Tenant_Code != null)
            {
                sqlSurch += " and Tenant_Code = '" + Model.Tenant_Code + "'";
            }
            if (Model.Tenant_LegalPerson != null)
            {
                sqlSurch += " and Tenant_LegalPerson like '%" + Model.Tenant_LegalPerson + "%'";
            }
            sqlSurch += " and Dt_Id = " + Dt_Id + " ";
            return where + sqlSurch;
        }
        #endregion

        #endregion
    }
}
